﻿Namespace Commands
    Public Class BuddyInformationUpdated
        Inherits CommandBase

        Private Const mRequestCode As String = "BuddyInformationUpdated"
        Private Const mReplyCode As String = ""

        Public Sub New()
            MyBase.New(mRequestCode, mReplyCode)
        End Sub

        Public Class Message
            Public Name As String
            Public Email As String
        End Class

        Friend Shared Sub Broadcast()
            Dim clsMsg As New Message
            clsMsg.Name = Settings.Nickname
            clsMsg.Email = Settings.Email

            Dim bMessage As Byte() = Framework.Serialization.ToXmlBytes(clsMsg)
            For Each clsNode As Framework.Net.Node In Framework.Net.Manager.GetAllOnline
                clsNode.SendCommandMessageAsync(mRequestCode, bMessage)
            Next
        End Sub

        Public Overrides Sub HandleRequest(ByVal envelope As Framework.Net.MessageEnvelope)
            Dim clsMsg As Message = Framework.Serialization.FromXmlBytes(Of Message)(envelope.Message.Message)

            envelope.Node.Buddy.Name = clsMsg.Name
            envelope.Node.Buddy.Email = clsMsg.Email
            Framework.Buddies.Manager.Save(envelope.Node.Buddy)
        End Sub

        Public Overrides Sub HandleReply(ByVal envelope As Framework.Net.MessageEnvelope)
            Throw New NotImplementedException
        End Sub

    End Class

End Namespace
